package org.bigdata.leo.hbase.starter.boot;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.bigdata.leo.hbase.starter.model.HBaseConnThreadPoolInit;
import org.bigdata.leo.hbase.starter.sdk.HBaseTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * @author leo.jie (weixiao.me@aliyun.com)
 * @version 1.0
 * @organization bigdata
 * @website https://www.jielongping.com
 * @date 2020/3/30 9:03 下午
 * @since 1.0
 */
@org.springframework.context.annotation.Configuration
@EnableConfigurationProperties(HBaseProperties.class)
@ConditionalOnClass(HBaseTemplate.class)
public class HBaseAutoConfiguration {
    private static final String HBASE_QUORUM = "hbase.zookeeper.quorum";
    private static final String HBASE_ROOTDIR = "hbase.rootdir";
    private static final String HBASE_ZNODE_PARENT = "zookeeper.znode.parent";


    private final HBaseProperties hBaseProperties;

    public HBaseAutoConfiguration(HBaseProperties hBaseProperties) {
        this.hBaseProperties = hBaseProperties;
    }

    @Bean
    @ConditionalOnMissingBean(HBaseTemplate.class)
    public HBaseTemplate hBaseTemplate() {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set(HBASE_QUORUM, this.hBaseProperties.getQuorum());
        configuration.set(HBASE_ROOTDIR, hBaseProperties.getRootDir());
        configuration.set(HBASE_ZNODE_PARENT, hBaseProperties.getNodeParent());
        HBaseConnThreadPoolInit hBaseConnThreadPoolInit = new HBaseConnThreadPoolInit();
        hBaseConnThreadPoolInit.setThreadPoolName(hBaseProperties.getThreadPoolName());
        hBaseConnThreadPoolInit.setCorePoolSize(hBaseProperties.getCorePoolSize());
        hBaseConnThreadPoolInit.setMaximumPoolSize(hBaseProperties.getMaximumPoolSize());
        hBaseConnThreadPoolInit.setKeepAliveTime(hBaseProperties.getKeepAliveTime());
        return new HBaseTemplate(configuration, hBaseConnThreadPoolInit);
    }
}
